<template>
  <div class="goods-hot">
    <h3>{{ title }}</h3>
    <!-- 商品区块 -->
    <good-item v-for="good in hotList" :key="good.id" :good="good"/>
  </div>
</template>
<script>
import GoodItem from '@/views/Category/components/good-item'
import { computed, onMounted, ref } from 'vue'
import { findHotGoods } from '@/api/good'
import { useRoute } from 'vue-router'

/**
 * 1. 根据传入的prop中的type类型 渲染不一样的title
 *    type -> title
 * 2. 根据传入的prop中的type类型 获取不一样的热榜数据
 *    把type作为接口字段
 */
export default {
  name: 'GoodsHot',
  components: {
    GoodItem
  },
  props: {
    type: {
      type: Number,
      default: 1
    }
  },
  setup (props) {
    // 使用枚举定义type和title对应关系
    const types = {
      1: '24小时热销榜',
      2: '周热销榜',
      3: '总热销榜'
    }
    const title = computed(() => {
      return types[props.type]
    })
    const route = useRoute()
    // 使用type获取接口数据
    const hotList = ref([])
    async function loadHotData () {
      const res = await findHotGoods({
        id: route.params.id,
        type: props.type
      })
      hotList.value = res.result
    }
    onMounted(() => {
      loadHotData()
    })
    return {
      title,
      hotList
    }
  }
}
</script>
<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
}
</style>
